﻿namespace KinonekoSoftware.Studio.Pages.Theories
{
    public sealed class NewVocabularyViewModel : ObjectDialog<Vocabulary>
    {

        public static Task<Result<Vocabulary>> Create()
        {
            return Dialog<Vocabulary>(new NewVocabularyViewModel());
        }

        public static Task<Result<Vocabulary>> Edit(Vocabulary target)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Value, target);
            return Dialog<Vocabulary>(new NewVocabularyViewModel(), context);
        }
        
        private string     _name;
        private string     _intro;
        private string     _background;
        private string     _borderBrush;
        private string     _metadata;
        private Vocabulary _target;

        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(DialogRule.Create<NewVocabularyViewModel>(CodariaSR.Text_Parameter_EmptyName, x => !string.IsNullOrEmpty(x.Name)));
        }

        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                _target     = context.GetObject<Vocabulary>(ContextBase.Key_Value);
                Metadata  = _target.Metadata;
                Name        = _target.Name;
                BorderBrush = _target.BorderBrush;
                Background  = _target.Background;
                Intro       = _target.Intro;
                return true;
            }

            return false;
        }

        protected override Vocabulary OnFinish(bool editing)
        {
            if (editing)
            {
                _target.Metadata  = Metadata;
                _target.Background  = Background;
                _target.BorderBrush = BorderBrush;
                _target.Intro       = Intro;
                _target.Name        = Name;
            }
            else
            {
                _target = new Vocabulary
                {
                    Id          = ID.Get(),
                    Metadata  = Metadata,
                    Background  = Background,
                    BorderBrush = BorderBrush,
                    Intro       = Intro,
                    Name        = Name,
                };
            }

            return _target;
        }


        public string Metadata
        {
            get => _metadata;
            set => TryFinishAndSetValue(ref _metadata, value);
        }

        public string BorderBrush
        {
            get => _borderBrush;
            set => TryFinishAndSetValue(ref _borderBrush, value);
        }

        public string Background
        {
            get => _background;
            set => TryFinishAndSetValue(ref _background, value);
        }


        public string Intro
        {
            get => _intro;
            set => TryFinishAndSetValue(ref _intro, value);
        }

        public string Name
        {
            get => _name;
            set => TryFinishAndSetValue(ref _name, value);
        }
    }
}